<template>
  <Content title="权限">
    <div slot="body">
      <el-form :inline="true">
        <el-form-item>
          <div style="color: #6c757d!important;">系统权限列表</div>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button type="success" @click="handleAdd" plain icon="el-icon-circle-plus">新增</el-button>
        </el-form-item>
      </el-form>
      <tree-grid :columns="columns" :tree-structure="true" :data-source="dataSource" v-loading="loading"
                 ref="treeGrid"
                 @handleEdit="handleEdit"
                 @handleChild="handleChild"
                 @handleDelete="handleDelete">
      </tree-grid>
    </div>
  </Content>
</template>

<script>
  import util from '../../utils/util'
  import Content from '../../components/Content.vue'
  import {TreeGrid} from '../../components/treeTable'
  import {mapState, mapActions} from 'vuex'
  import {Permission} from '../../utils/resource'
  import {deleteConfirm} from '../../utils/element-helper'


  export default {
    name: 'permission',
    components: {
      Content: Content,
      TreeGrid: TreeGrid
    },
    data() {
      return {
        loading: false,
        columns: [
          {
            text: '名称',
            dataIndex: 'name'
          },
          {
            text: '编码',
            dataIndex: 'code'
          },
          {
            text: '优先级',
            dataIndex: 'priority'
          },
          {
            text: '类型',
            dataIndex: 'type'
          },
        ],
        dataSource: [],
      }
    },
    created() {
      this.loadTree()
    },
    computed: {},
    methods: {
      ...mapActions("permission", [
        "setPermissions",
      ]),
      async loadTree() {
        this.loading = true
        this.dataSource = []
        let res = await Permission.tree()
        Object.assign(this.dataSource, res.data)
        this.permissions = res.data
        this.$refs.treeGrid.loadData()
        this.setPermissions(res.data)
        this.$nextTick(() => {
          this.loading = false
        })
      },
      handleAdd() {
        this.$router.push({path: "/permission/new"})
      },
      handleEdit(index, row, id) {
        this.$router.push({path: "/permission/" + row.id})
      },
      handleChild(index, row, parent) {
        this.$router.push({path: "/permission/new/" + row.id})
      },
      handleDelete(index, row, id) {
        deleteConfirm(this, {_content: "删除权限将一并删除所有子权限, 是否继续?"}, () => Permission.del(row.id), (resp) => this.loadTree())
      }
    },
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>


</style>
